# -*- coding: utf8 -*-

from project.sqlite3_db.table import Table

class TableHash( Table ):

    _table_name = "hash"

    def create( self ):
        cur = self.getCursor()

        # Create table
        cur.execute(
            "CREATE TABLE IF NOT EXISTS hash ("+
            "id INTEGER PRIMARY KEY, "+
            "value TEXT)"
        )

        cur.close()
        return


    def insert_hash( self, value ):
        cur = self.getCursor()

        cur.execute("INSERT INTO hash(value) VALUES (?)", (value,))
        hash_id = cur.lastrowid

        self.getConnection().commit()

        cur.close()
        return hash_id


    def find_hash( self, value ):
        cur = self.getCursor()
        cur.execute("SELECT * FROM hash WHERE value=:value",
                {"value": value}
        )
        item = cur.fetchone()
        cur.close()
        if not item:
            item = False
        return item

    def get_by_ids( self, hash_ids ):
        cur = self.getCursor()

        hashes = []
        if len(hash_ids):
            cur.execute("SELECT * FROM hash WHERE id IN (" + ",".join(hash_ids) + ")" )
            hashes = cur.fetchall()

        cur.close()
        return hashes

    pass
